import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ProductDetailPage extends JPanel {
    public ProductDetailPage(CardLayout cardLayout, JPanel cardPanel) {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);

        // 返回按钮
        JPanel headerPanel = createHeaderPanel(cardLayout, cardPanel);
        add(headerPanel, BorderLayout.NORTH);

        // 商品详情内容
        JPanel detailPanel = createDetailPanel();
        JScrollPane scrollPane = new JScrollPane(detailPanel);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(scrollPane, BorderLayout.CENTER);

        // 底部操作栏
        JPanel bottomPanel = createBottomPanel(cardLayout, cardPanel);
        add(bottomPanel, BorderLayout.SOUTH);
    }

    private JPanel createHeaderPanel(CardLayout cardLayout, JPanel cardPanel) {
        JPanel headerPanel = new JPanel(new BorderLayout());
        headerPanel.setBackground(new Color(240, 240, 240));
        headerPanel.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));

        JButton backButton = new JButton("← 返回");
        backButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        backButton.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        backButton.setBackground(new Color(240, 240, 240));
        backButton.addActionListener(e -> cardLayout.show(cardPanel, "productList"));
        headerPanel.add(backButton, BorderLayout.WEST);

        return headerPanel;
    }

    private JPanel createDetailPanel() {
        JPanel detailPanel = new JPanel();
        detailPanel.setLayout(new BoxLayout(detailPanel, BoxLayout.Y_AXIS));
        detailPanel.setBackground(Color.WHITE);
        detailPanel.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20));

        // 商品标题
        JLabel titleLabel = new JLabel("商品介绍");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 15, 0));
        detailPanel.add(titleLabel);

        // 商品规格
        String[] specs = {
            "型号：iPhone 11",
            "存储容量：128GB",
            "颜色：黑色",
            "网络锁：无锁（支持全网通）",
            "保修状态：已过保（可提供购买凭证）",
            "电池健康：86%（截图可提供）",
            "配件：原装充电线+第三方充电头。"
        };
        
        for (String spec : specs) {
            JLabel label = new JLabel(spec);
            label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            label.setBorder(BorderFactory.createEmptyBorder(3, 0, 3, 0));
            detailPanel.add(label);
        }

        // 附加价值
        JLabel addValueLabel = new JLabel("附加价值：");
        addValueLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
        addValueLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0));
        detailPanel.add(addValueLabel);

        JLabel giftLabel = new JLabel("赠送3个手机壳+钢化膜备用。");
        giftLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        giftLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
        detailPanel.add(giftLabel);

        // 商品描述
        String[] descriptions = {
            "这是一个使用过一年的iphone 11，屏幕略有划痕，",
            "前后摄像头拍照清晰，可以正常使用，",
            "电池最大容量还有80%，边框略有磕碰，",
            "其他零部件原厂未拆过，均完好无损。"
        };
        
        for (String desc : descriptions) {
            JLabel label = new JLabel(desc);
            label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            label.setBorder(BorderFactory.createEmptyBorder(3, 0, 3, 0));
            detailPanel.add(label);
        }

        // 商品图片预览
        JPanel imagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        imagePanel.setBackground(Color.WHITE);
        imagePanel.setBorder(BorderFactory.createEmptyBorder(15, 0, 15, 0));
        
        for (int i = 0; i < 3; i++) {
            JLabel imageLabel = new JLabel(new ImageIcon(createProductImage(100, 100)));
            imageLabel.setBorder(BorderFactory.createLineBorder(new Color(220, 220, 220)));
            imagePanel.add(imageLabel);
        }
        
        detailPanel.add(imagePanel);

        return detailPanel;
    }

    private Image createProductImage(int width, int height) {
        // 创建一个简单的商品图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setColor(new Color(230, 240, 250));
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(Color.DARK_GRAY);
        g2d.drawString("iPhone 11", width/2-25, height/2);
        g2d.dispose();
        return image;
    }

    private JPanel createBottomPanel(CardLayout cardLayout, JPanel cardPanel) {
        JPanel bottomPanel = new JPanel(new GridLayout(1, 4, 1, 0));
        bottomPanel.setBackground(new Color(250, 250, 250));
        bottomPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(230, 230, 230)));

        String[] buttons = {"收藏", "留言", "分享", "询问购买"};
        for (String text : buttons) {
            JButton button = new JButton(text);
            button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            button.setBackground(Color.WHITE);
            button.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
            
            if (text.equals("询问购买")) {
                button.setBackground(new Color(255, 80, 80));
                button.setForeground(Color.WHITE);
                button.addActionListener(e -> cardLayout.show(cardPanel, "purchaseInfo"));
            }
            
            bottomPanel.add(button);
        }
        return bottomPanel;
    }
}